<?php
$TVvalues = !empty($TVvalues) ? $TVvalues : "";
$TVvalue = explode(", ",$TVvalues);
$firstclass = !empty($firstclass) ? $firstclass : "";
$firstclassend = !empty($firstclassend) ? $firstclassend : "";
$lastclass = !empty($lastclass) ? $lastclass : "";
$lastclassend = !empty($lastclassend) ? $lastclassend : "";
$rowLimit = !empty($rowLimit) ? $rowLimit : 1;
$div = !empty($div) ? $div : "";
$enddiv = !empty($enddiv) ? $enddiv : "";
$parents = !empty($parents) ? $parents : $modx->resource->get('id');
$where = !empty($where) ? $modx->fromJSON($where) : array();
$Query = $modx->newQuery('modResource');
$Query->where($where);
$Query->where(array(
    'parent' => $parents,
    'published' => 1,
    'deleted' => 0,
));
$total=$modx->getCount("modResource", $Query);
$totalVar=$modx->getOption('totalVar', $scriptProperties, 'total');
$modx->setPlaceholder($totalVar,$total);
$limit=$modx->getOption('limit', $scriptProperties, 10);
$offset=$modx->getOption('offset', $scriptProperties, 0);
$Query->limit($limit, $offset);
$childrens =  $modx->getCollection('modResource', $Query);
$i = 1;
$output .= $div;
foreach ($childrens as $child) {
    foreach ($TVvalue as $tv){
        $tvs[$tv] = $child->getTVValue($tv);
    }
    if ($i % $rowLimit == 1) {
        $output .= $firstclass .  $modx->getChunk($tpl,array_merge($child->toArray(),$tvs)) . $firstclassend;
    }
    else if ($i % $rowLimit == 0) {
        $output .= $lastclass . $modx->getChunk($tpl,array_merge($child->toArray(),$tvs)) . $lastclassend;
        $output .= $enddiv;
        $output .= $div;
    }
    else {
        $output .= $modx->getChunk($tpl,array_merge($child->toArray(),$tvs));
    }
    $i++;
}
$output .= $enddiv;
return $output;